# -*- coding: utf-8 -*-
# Part of Odoo. See LICENSE file for full copyright and licensing details.

import logging

from odoo import api, fields, models, _
from odoo.exceptions import AccessDenied, UserError

_logger = logging.getLogger(__name__)

class ProjectProject(models.Model):
    _inherit = "project.project"
    limit_time = fields.Datetime(string='limit_time' )


class ProjectTask(models.Model):
    _inherit = "project.task"

    notes = fields.Text()
    start_time = fields.Datetime(string='start_time', index=True )
    end_time = fields.Datetime(string='end_time', index=True )

    @api.model
    @api.returns('self', lambda value: value.id)    
    def create_for_dispatch(self, values):
        project_id = self.env['ir.model.data'].xmlid_to_res_id('cr_train_schedule.project_dispatch', True)
        stage_id = self.env['ir.model.data'].xmlid_to_res_id('cr_train_schedule.project_stage_data_draft', True)
        values['project_id'] = project_id
        values['stage_id'] = stage_id
        return self.create(values)

    def action_ready(self):
        stage_id = self.env['ir.model.data'].xmlid_to_res_id('cr_train_schedule.project_stage_data_ready', True)
        return self.write({'stage_id':stage_id })

    def action_done(self):
        stage_id = self.env['ir.model.data'].xmlid_to_res_id('cr_train_schedule.project_stage_data_done', True)
        return self.write({'stage_id':stage_id })

    def action_draft(self):
        stage_id = self.env['ir.model.data'].xmlid_to_res_id('cr_train_schedule.project_stage_data_draft', True)
        return self.write({'stage_id':stage_id })

    @api.model
    def set_dispatch_limit_time (self, limit ):
        project_id = self.env['ir.model.data'].xmlid_to_res_id('cr_train_schedule.project_dispatch', True)
        prj = self.env['project.project'].browse(project_id)
        prj.limit_time = limit
        return True

    @api.model
    def get_dispatch_limit_time (self ):
        project_id = self.env['ir.model.data'].xmlid_to_res_id('cr_train_schedule.project_dispatch', True)
        prj = self.env['project.project'].browse(project_id)
        return prj.limit_time


